const express = require("express");
const router = express.Router();

const classDB = require("../../schemas/class");
const liveDB = require("../../schemas/live");

// 新增直播
router.post("/addLive", async (req, res) => {
  const {
    // 直播标题
    liveName,
    // 直播时长
    liveDuration,
    // 直播讲师
    teacher,
    // 直播时间
    liveTime,
    className,
    courseName,
    _id,
  } = req.body;
  if (
    className &&
    liveName &&
    _id &&
    liveName &&
    liveDuration &&
    teacher &&
    courseName
  ) {
    // 哪个为空都不行  全都有值才能走进来
    try {
      // 中间
      let data = await liveDB.findByIdAndUpdate(
        { _id },
        {
          $push: {
            liveList: {
              className,
              courseName,
              liveName,
              teacher,
              liveTime: new Date(liveTime) / 1,
              liveDuration: liveDuration / 1,
            },
          },
        }
      );
      // console.log(data);
    } catch (error) {
      res.send({
        code: 1,
        txt: "新增直播失败",
      });
    }

    res.send({ code: 0, txt: "新增直播成功" });
  } else {
    res.send({ code: 1, txt: "新增直播失败" });
  }
});

// 删除直播
router.post("/deleteLive", async (req, res) => {
  const { _id, id } = req.body;
  try {
    await liveDB.findByIdAndUpdate(
      { _id },
      {
        $pull: {
          liveList: { _id: id },
        },
      }
    );
  } catch (error) {
    return res.send({ code: 1, txt: "删除失败" });
  }
  res.send({ code: 0, txt: "删除成功" });
});

// 修改直播信息
router.post("/reviseLive", async (req, res) => {
  const {
    // 直播标题
    liveName,
    // 直播时长
    liveDuration,
    // 直播讲师
    teacher,
    // 直播时间
    liveTime,
    _id,
    id,
  } = req.body;
  if (liveTime && _id && liveName && liveDuration && teacher && id) {
    const data = await liveDB.findById({ _id });
    // console.log(data);
    if (data) {
      const reviseObj = data.liveList.find((item) => {
        return item._id.toString() === id;
      });
      // console.log(reviseObj);
      reviseObj.liveDuration = liveDuration;
      reviseObj.teacher = teacher;
      reviseObj.liveTime = liveTime;
      reviseObj.liveName = liveName;

      // console.log(reviseObj, "---------", data.liveList);
      liveDB
        .findByIdAndUpdate({ _id }, { liveList: data.liveList })
        .then(() => {
          res.send({ code: 0, txt: "修改直播成功" });
        })
        .catch(() => {
          res.send({ code: 1, txt: "修改直播失败" });
        });
    }
  } else {
    res.send({ code: 1, txt: "修改直播失败" });
  }
});

// 获取直播
router.get("/liveList", async (req, res) => {
  const { liveName, liveTime, pageSize, currentPage } = req.query;
  let data = await liveDB.find();

  data = data.filter(
    (item) => item.liveList[0].className === "web-前端进阶框架-朱雀"
  )[0];
  // 筛选时间
  if (liveTime) {
    let startTime = new Date(liveTime[0]) / 1;
    let endTime = new Date(liveTime[1]) / 1;

    data.liveList = data.liveList.filter(
      (item) => item.liveTime >= startTime && item.liveTime <= endTime
    );
  }

  // 筛选直播名
  if (liveName) {
    data.liveList = data.liveList.filter((item) =>
      item.liveName.includes(liveName)
    );
  }

  const total = data.liveList.length;

  data.liveList.sort((a, b) => b.liveTime - a.liveTime);

  data.liveList = data.liveList.splice((currentPage-1)*pageSize,pageSize)

  // console.log(data);

  res.send({
    code: 0,
    total,
    data
  });
});

module.exports = router;
